const Index = {
	init(option){
		const { onMsg } = option ?? {}
		// const url = '110.185.170.23:8282/'
		const url = '192.168.0.106:8282/'
		// const url = '47.108.62.210:8688/'
		const socket = new WebSocket(`ws://${url}`)
		
		const { wstoken, uuid } = window.$fn.getUser()
		
		const query = { act:'login', token:wstoken, uid:uuid }
		
		// 创建 WebSocket 连接 
		socket.addEventListener('open', function (event) {
			console.log('sockey success connect')
		    socket.send(JSON.stringify(query));
		})
		// 监听数据
		socket.addEventListener('message', function (event) {
		    onMsg?.(event.data)
			if(event.data){
				const data = JSON.parse(event.data)
				const { msgtype, msg } = data
				window.$fn.loading(false)
				window.frameRef.setState({num : 0})
				if( msgtype && msg ){
					switch (msgtype) {
						case 1:
							window.$fn.notice.success(msg.title, '系统提示')
							break;
						case 2:
							window.$fn.notice.warning(msg.title, '系统提示')
							break;
						case 3:
							window.$fn.notice.warning(msg.title, '系统提示', { download_url: msg.download_url, placement: 'bottomRight', duration: null, btn: {label: '点击下载'}, noClose: true })
							break;
						case 4:
							window.systemRef?.open(msg)
							break;
						default:
							break;
					}
					window?.onRefresh?.()
					window?.modal?.destroy?.()
					window.frameRef.setState({num: msg.msg_count})
				}
			}
		})
		return socket
	},
	close(socket){
		socket.close()
	}
}

export default Index